package com.ccsc.auth.controller;

import com.ccsc.auth.entity.Position;
import com.ccsc.auth.entity.vi.PositionVi;
import com.ccsc.auth.entity.vo.PositionVo;
import com.ccsc.auth.service.PositionService;
import com.ccsc.auth.utils.BaseResponse;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by yyangs on 2017/2/17.
 */
@RestController
@RequestMapping("/positionManage")
public class PositionController extends BaseController {
    @Autowired
    private PositionService positionService;

    //获取全部职位信息
    @RequestMapping(value = "/positions", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getAllPosition(WebRequest request) throws Exception {
        try {
            List<PositionVo> posList = Lists.newArrayList();
            List<Position> positionList = positionService.getAllPositions();
            if(positionList!=null && positionList.size()>0){
                for(Position position : positionList){
                    PositionVo positionVo = position2PositionVo(position);
                    posList.add(positionVo);
                }
            }
            return this.ok(posList);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //查询职位详细信息根据id
    @RequestMapping(value = "/position/{positionIdIn}", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getPositionById(@PathVariable("positionIdIn")String positionIdIn) throws Exception {
        Integer positionId=Integer.parseInt(positionIdIn);
        try {
            Position position=positionService.getPositionById(positionId);
            PositionVo positionVo = new PositionVo();
            BeanUtils.copyProperties(positionVo, position);
            return this.ok(positionVo);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //添加新职位
    @RequestMapping(value = "/position", method = RequestMethod.POST, produces = {"application/json", "application/xml"})
    public BaseResponse addPosition(@Valid @RequestBody PositionVi positionVi, BindingResult bindingResult) throws Exception {
        try {
            Integer result=0;
            Position position = positionVi2Position(positionVi);
            List<Position> positionList=positionService.getPositionsByName(position.getName());
            if(positionList.size()>0) {
                return this.error(500, "职位名称已存在，添加失败");
            }
            result = positionService.createPosition(position);
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //修改职位
    @RequestMapping(value = "/position/{positionIdIn}", method = RequestMethod.PUT, produces = {"application/json", "application/xml"})
    public BaseResponse updatePosition(@PathVariable("positionIdIn")String positionIdIn, @Valid @RequestBody PositionVi positionVi, BindingResult bindingResult) throws Exception {
        try {
            Integer result=0;
            positionVi.setId(Integer.parseInt(positionIdIn));
            Position positionTemp=positionService.getPositionById(positionVi.getId());
            if(positionTemp == null) {
                return this.error(500, "职位不存在，修改失败");
            }
            Position position = positionVi2Position(positionVi);
            List<Position> positionList=positionService.getPositionsByName(position.getName());
            if(positionList.size()>0) {
                return this.error(500, "职位名称已存在，修改失败");
            }
            result = positionService.updatePosition(position);
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //删除职位
    @RequestMapping(value = "/position/{positionIdIn}", method = RequestMethod.DELETE, produces = {"application/json", "application/xml"})
    public BaseResponse deletePosition(@PathVariable("positionIdIn")String positionIdIn) throws Exception {
        Integer positionId=Integer.parseInt(positionIdIn);
        try {
            Integer result=0;
            Position position = positionService.getPositionById(positionId);
            if(position!=null) {
                result= positionService.deletePositionById(positionId);
            }
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }
    public static Position positionVi2Position(PositionVi positionVi){
        Position position=new Position();
        position.setId(positionVi.getId());
        position.setName(positionVi.getName());
        return position;
    }
    public static PositionVo position2PositionVo(Position position){
        PositionVo positionVo=new PositionVo();
        positionVo.setId(position.getId());
        positionVo.setName(position.getName());
        return positionVo;
    }

}
